<?php
namespace App\Models\FinanceReport;

use App\Models\Basics\SysCountry;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;
class PurchaseAmount extends Model
{
    protected $table = 'fr_purchase_amount';

    protected $guarded = [];

    public $timestamps = false;

    public function country(){
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public static function headersField(){
        $fields = [];
        $fields['sku'] = ['title' => 'SKU','sumField' => false];
        $fields['sku_cn'] = ['title' => '产品名称','sumField' => false];
        $fields['country'] = ['title' => '国家','sumField' => false];
        $fields['pu_num'] = ['title' => '采购数量','sumField' => true];
        $fields['pu_money'] = ['title' => '采购金额','sumField' => true];
        return $fields;
    }

    public static function getDownloadData($re){
        foreach ($re as $row){
            $temp = [];
            $temp['psku_code'] = $row->sku->psku_code ?? '';
            $temp['psku_cn'] = $row->sku->psku_cn ?? '';
            $temp['country'] = $row->country->country ?? '';
            $temp['pu_num'] = $row->pu_num;
            $temp['pu_money'] = $row->pu_money;
            yield $temp;
        }
    }
}
